<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Java流程 | 小谭的部落阁</title><meta name="keywords" content="Java,流程控制"><meta name="author" content="谭自财"><meta name="copyright" content="谭自财"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Java流程输入类-Scannerscanner, java.util.Scanner，在java5的新特性，我们可以通过Scanner类来获取用户的输入。 基本的语法：  Scanner s &#x3D; new Scanner([System.in](http:&#x2F;&#x2F;System.in));   通过Scanner类的netx()和nextLine()方法来获取输入的字符串，在读取前我们一般需要使用has">
<meta property="og:type" content="article">
<meta property="og:title" content="Java流程">
<meta property="og:url" content="https://tanzicai.gitee.io/2022/02/09/Java%E6%B5%81%E7%A8%8B/index.html">
<meta property="og:site_name" content="小谭的部落阁">
<meta property="og:description" content="Java流程输入类-Scannerscanner, java.util.Scanner，在java5的新特性，我们可以通过Scanner类来获取用户的输入。 基本的语法：  Scanner s &#x3D; new Scanner([System.in](http:&#x2F;&#x2F;System.in));   通过Scanner类的netx()和nextLine()方法来获取输入的字符串，在读取前我们一般需要使用has">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/tanzicai/file-pan/raw/master/img/20220209170711.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg">
<meta property="article:published_time" content="2022-02-09T11:25:00.000Z">
<meta property="article:modified_time" content="2022-02-09T09:07:27.989Z">
<meta property="article:author" content="谭自财">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="流程控制">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/tanzicai/file-pan/raw/master/img/20220209170711.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://tanzicai.gitee.io/2022/02/09/Java%E6%B5%81%E7%A8%8B/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Java流程',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-02-09 17:07:27'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const fontSizeVal = saveToLocal.get('global-font-size')
    if (fontSizeVal !== undefined) {
      document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
    }
    })(window)</script><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/atom.xml" title="小谭的部落阁" type="application/atom+xml">
</head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="https://gitee.com/tanzicai/drawingbed/raw/master/img/20210609205810.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">35</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">12</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标籤</span></a></div><div class="menus_item"><a class="site-page" href="/category/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/fridends/"><i class="fa-fw fa fa-heartbeat"></i><span> 朋友圈</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/download/"><i class="fa-fw fas fa--cloud-download"></i><span> 下载</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-hear"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gitee.com/tanzicai/file-pan/raw/master/img/20220209170711.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">小谭的部落阁</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标籤</span></a></div><div class="menus_item"><a class="site-page" href="/category/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/fridends/"><i class="fa-fw fa fa-heartbeat"></i><span> 朋友圈</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/download/"><i class="fa-fw fas fa--cloud-download"></i><span> 下载</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-hear"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Java流程</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-02-09T11:25:00.000Z" title="发表于 2022-02-09 19:25:00">2022-02-09</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-02-09T09:07:27.989Z" title="更新于 2022-02-09 17:07:27">2022-02-09</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%90%8E%E7%AB%AF/">后端</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Java流程"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Java流程"><a href="#Java流程" class="headerlink" title="Java流程"></a>Java流程</h1><h1 id="输入类-Scanner"><a href="#输入类-Scanner" class="headerlink" title="输入类-Scanner"></a>输入类-Scanner</h1><p><code>scanner</code>,<code> java.util.Scanner</code>，在<code>java5</code>的新特性，我们可以通过<code>Scanner</code>类来获取用户的输入。</p>
<p>基本的语法： </p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">Scanner s = <span class="keyword">new</span> Scanner([System.in](http:<span class="comment">//System.in));</span></span><br></pre></td></tr></table></figure>


<p>通过Scanner类的<code>netx()</code>和<code>nextLine()</code>方法来获取输入的字符串，在读取前我们一般需要使用<code>hasNext()</code>和<code>hasNextLine()</code>判断是否还有输入的数据</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo1</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> a</span>&#123;</span><br><span class="line">        <span class="comment">//创建一个扫描器对象，用于接收键盘数据</span></span><br><span class="line">        Scanner scanner =<span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        System.out.println(<span class="string">&quot;使用next方式进行接收:&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//判断用户有没有输入字符串</span></span><br><span class="line">        <span class="keyword">if</span>(scanner.hasNext())&#123;</span><br><span class="line">            String str = scanner.next();</span><br><span class="line">            System.out.println(<span class="string">&quot;输入的内容为:&quot;</span>+str);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//凡是属于IO流的类，用完要关掉</span></span><br><span class="line">        scanner.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<ul>
<li>next()<ol>
<li>一定要读取到有效字符后才可以结束输入。</li>
<li>对输入有效字符之前遇到空白，netx()方法会自动将其去掉</li>
<li>只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符</li>
<li>next()不能得到带有空格的字符串</li>
</ol>
</li>
<li>nextLine()<ol>
<li>以Enter为结束符，也就是说，nextLine()方法返回的是输入回车之前的所有字符。</li>
<li>可以获得空白</li>
</ol>
</li>
</ul>
<h2 id="Scanner实例"><a href="#Scanner实例" class="headerlink" title="Scanner实例"></a>Scanner实例</h2><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo2</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> i = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">float</span> f = <span class="number">0.0f</span>;</span><br><span class="line"></span><br><span class="line">        Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;请输入一个整数数据:&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(scanner.hasNextInt())&#123;</span><br><span class="line">            i = scanner.nextInt();</span><br><span class="line">            System.out.println(<span class="string">&quot;你输入的整数是:&quot;</span>+i);</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;你输入的不是整数数据!&quot;</span>+i);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>多次循环的结构</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo5</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//我们输入多个数字，并求其总和和平均数，每输入一个数字用回车确认，通过输入非数字来结束输入并输出执行结果</span></span><br><span class="line">        Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        <span class="keyword">double</span> sum = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> m =<span class="number">0</span>;</span><br><span class="line">        <span class="comment">//通过循环判断是否还有输入，并且里面对每一次进行求和和统计</span></span><br><span class="line">        <span class="keyword">while</span> (scanner.hasNextDouble())&#123;</span><br><span class="line">            <span class="keyword">double</span> x =scanner.nextDouble();</span><br><span class="line">            System.out.println(<span class="string">&quot;你输入了第&quot;</span>+(m+<span class="number">1</span>)+<span class="string">&quot;个数据, 然后当前结果sum=&quot;</span>+sum);</span><br><span class="line">            m = m+<span class="number">1</span>; <span class="comment">//m++</span></span><br><span class="line">            sum = sum+x;</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(m+<span class="string">&quot;个数的和为&quot;</span>+sum);</span><br><span class="line">        System.out.println(m+<span class="string">&quot;个数的平均值是:&quot;</span>+(sum/m));</span><br><span class="line"></span><br><span class="line">        scanner.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h2 id="顺序结构"><a href="#顺序结构" class="headerlink" title="顺序结构"></a>顺序结构</h2><p>语句与语句之间，框与框之间按照从上到下顺序进行的，他是若干次</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> struct;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ShunXuDemo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Hello World1!&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;Hello World2!&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;Hello World3!&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;Hello World4!&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;Hello World5!&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h2 id="选择结构"><a href="#选择结构" class="headerlink" title="选择结构"></a>选择结构</h2><h3 id="if-单选择结构"><a href="#if-单选择结构" class="headerlink" title="if 单选择结构"></a>if 单选择结构</h3><p>我们很多时候需要去判断一个东西是否可行，然后我们才去执行，这样一个过程在程序中用if语句来表示</p>
<p>语法结构：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">if</span>(布尔表达式)&#123;</span><br><span class="line">    <span class="comment">//如果布尔表达式为true将执行的语句</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>示例代码：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> struct;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ifDemo1</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        System.out.println(<span class="string">&quot;请输入内容:&quot;</span>);</span><br><span class="line">        String scanner1 = scanner.nextLine();</span><br><span class="line">        <span class="comment">//equals ：判断字符串是否相等</span></span><br><span class="line">        <span class="keyword">if</span>(scanner1.equals(<span class="string">&quot;Hello&quot;</span>))&#123;</span><br><span class="line">            System.out.println(scanner1);</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>String中的equals方法</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">equals</span><span class="params">(Object anObject)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">this</span> == anObject) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (anObject <span class="keyword">instanceof</span> String) &#123;</span><br><span class="line">            String anotherString = (String) anObject;</span><br><span class="line">            <span class="keyword">int</span> n = value.length;</span><br><span class="line">            <span class="keyword">if</span> (n == anotherString.value.length) &#123;</span><br><span class="line">                <span class="keyword">char</span> v1[] = value;</span><br><span class="line">                <span class="keyword">char</span> v2[] = anotherString.value;</span><br><span class="line">                <span class="keyword">int</span> i = <span class="number">0</span>;</span><br><span class="line">                <span class="keyword">while</span> (n-- != <span class="number">0</span>) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (v1[i] != v2[i])</span><br><span class="line">                            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">                    i++;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>


<h3 id="if-双选择结构"><a href="#if-双选择结构" class="headerlink" title="if 双选择结构"></a>if 双选择结构</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">if</span>(布尔表达式)&#123;</span><br><span class="line">  <span class="comment">//如果布尔表达式的值为true</span></span><br><span class="line">&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">  <span class="comment">//如果布尔表达式的值为false</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> struct;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ifDemo2</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//考试分数大于60分就是及格，小于60分就是不及格</span></span><br><span class="line">        Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        System.out.println(<span class="string">&quot;请输入成绩:&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">int</span> score = scanner.nextInt();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(score&gt;<span class="number">60</span>)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;及格&quot;</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;不及格&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="if-多选择结构"><a href="#if-多选择结构" class="headerlink" title="if 多选择结构"></a>if 多选择结构</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">if</span>(布尔表达式 <span class="number">1</span>)&#123;</span><br><span class="line">    <span class="comment">//如果布尔表达式 1的值为true执行代码</span></span><br><span class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span>(布尔表达式 <span class="number">2</span>)&#123;</span><br><span class="line">    <span class="comment">//如果布尔表达式 2的值为true执行代码</span></span><br><span class="line">&#125;<span class="keyword">else</span> <span class="keyword">if</span>(布尔表达式 <span class="number">3</span>)&#123;</span><br><span class="line">    <span class="comment">//如果布尔表达式 3的值为true执行代码</span></span><br><span class="line">&#125;<span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="comment">//如果以上布尔表达式都不为true执行这个代码</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>只要一个语句满足，其他就不会执行了。</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> struct;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">IfDemo3</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        System.out.println(<span class="string">&quot;请输入成绩:&quot;</span>);</span><br><span class="line">        <span class="keyword">int</span> score = scanner.nextInt();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (score ==<span class="number">100</span>)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;恭喜满分!&quot;</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span> (score&gt;=<span class="number">90</span>&amp;&amp;score &lt; <span class="number">100</span>)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;A 级别&quot;</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span> (score&gt;=<span class="number">80</span>&amp;&amp;score &lt; <span class="number">90</span>)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;B 级别&quot;</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span> (score&gt;=<span class="number">70</span>&amp;&amp;score &lt; <span class="number">80</span>)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;C级别&quot;</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span> (score&gt;=<span class="number">60</span>&amp;&amp;score &lt; <span class="number">70</span>)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;D 级别&quot;</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span> (score&gt;=<span class="number">0</span>&amp;&amp;score &lt; <span class="number">60</span>)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;不及格&quot;</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;成绩不合法!&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="嵌套的if结构"><a href="#嵌套的if结构" class="headerlink" title="嵌套的if结构"></a>嵌套的if结构</h3><p>语法  </p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">if</span>(布尔表达式 <span class="number">1</span>)&#123;</span><br><span class="line">  <span class="comment">//如果布尔表达式 1 的值为true执行代码</span></span><br><span class="line">  <span class="keyword">if</span>(布尔表达式 <span class="number">2</span>)&#123;</span><br><span class="line">  <span class="comment">//如果布尔表达式 2的值为true执行代码</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="switch多选择结构"><a href="#switch多选择结构" class="headerlink" title="switch多选择结构"></a>switch多选择结构</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">switch</span>(expression )&#123;</span><br><span class="line">  <span class="comment">// case穿透  //switch匹配一个具体的值</span></span><br><span class="line">  <span class="keyword">case</span> value:</span><br><span class="line">        <span class="comment">//语句</span></span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> value:</span><br><span class="line">        <span class="comment">//语句</span></span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> value:</span><br><span class="line">      <span class="comment">//语句</span></span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">default</span>: <span class="comment">//可选语句</span></span><br><span class="line">        <span class="comment">//语句</span></span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> struct;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SwitchDemo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// case 穿透，//switch 匹配一个具体的值</span></span><br><span class="line">        <span class="keyword">char</span> grade = <span class="string">&#x27;C&#x27;</span>;</span><br><span class="line">        <span class="keyword">switch</span> (grade)&#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&#x27;A&#x27;</span>:</span><br><span class="line">                System.out.println(<span class="string">&quot;优秀&quot;</span>);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&#x27;B&#x27;</span>:</span><br><span class="line">                System.out.println(<span class="string">&quot;良好&quot;</span>);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&#x27;C&#x27;</span>:</span><br><span class="line">                System.out.println(<span class="string">&quot;良好&quot;</span>);</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&#x27;D&#x27;</span>:</span><br><span class="line">                System.out.println(<span class="string">&quot;良好&quot;</span>);</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&#x27;E&#x27;</span>:</span><br><span class="line">                System.out.println(<span class="string">&quot;挂科&quot;</span>);</span><br><span class="line"></span><br><span class="line">            <span class="keyword">default</span>:</span><br><span class="line">                System.out.println(<span class="string">&quot;良好&quot;</span>);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>switch语句中的变量类型可以是：</p>
<p>byte shore int 或者char,</p>
<p><strong>在jdk 1.7 以后开始，switch支持字符串String类型了</strong></p>
<h2 id="循环结构"><a href="#循环结构" class="headerlink" title="循环结构"></a>循环结构</h2><h3 id="while循环"><a href="#while循环" class="headerlink" title="while循环"></a>while循环</h3><p>语法结构</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">while</span>(布尔表达式)&#123;</span><br><span class="line">  <span class="comment">// 循环体表达式 </span></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<p>这个语法的规则是:</p>
<ol>
<li>只要布尔表达式内容为true，循环就一直执行下去</li>
<li>ww我们大多数情况下会让循环停止下来，我们需要一个让表达式失效的方式来结束循环</li>
<li>少部分情况需要循环一直执行，比如服务器的请求响应监听等</li>
<li>循环条件一直为true，就会造成一直循环，也就是死循环，我们在正常业务编程中需要尽量里面死循环，会影响程序性能或者造成程序卡死崩溃</li>
</ol>
<p>示例代码：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> struct;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">WhileDemo1</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//实现一个简单的 计算1+2+3+.....100=?</span></span><br><span class="line">        <span class="keyword">int</span> i = <span class="number">1</span>, sum = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span> (i &lt;= <span class="number">100</span>) &#123;</span><br><span class="line">            sum = sum + i;</span><br><span class="line">            i++;</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(sum);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="do…while循环"><a href="#do…while循环" class="headerlink" title="do…while循环"></a>do…while循环</h3><p>语法结构</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">do</span>&#123;</span><br><span class="line">  <span class="comment">//循环体代码</span></span><br><span class="line">&#125;<span class="keyword">while</span>(布尔表达式);</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<p>while和do…while的区别：</p>
<ol>
<li>while是先判断，后执行，dowhile是先执行后判断!</li>
<li>do…while总是保证循环体至少被执行一次，这是主要差别</li>
</ol>
<p>实例代码:</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> struct;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DoWhile2</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> a = <span class="number">0</span>, sum = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span> (a&lt;<span class="number">0</span>)&#123;</span><br><span class="line">            a++;</span><br><span class="line">            System.out.println(a);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;===========&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">do</span> &#123;</span><br><span class="line">            sum = sum + a;</span><br><span class="line">            a++;</span><br><span class="line">        &#125; <span class="keyword">while</span> (a &lt;= <span class="number">100</span>);</span><br><span class="line">        System.out.println(sum);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="for循环"><a href="#for循环" class="headerlink" title="for循环"></a>for循环</h3><p>虽然所有的循环结构都可以使用while和do…while表示，但是java提供了一个更简单的for循环结构，for循环语句是一种支持迭代的通用结构，是一种最灵活，最有效的循环结构</p>
<p>for循环执行的次数是在执行前就确定的，语法：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">for</span>(初始化； 布尔表达式； 迭代)&#123;</span><br><span class="line">  <span class="comment">//循环体代码语句</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>示例代码</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> struct;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ForDemo1</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> oddSum = <span class="number">0</span>; <span class="comment">//奇数</span></span><br><span class="line">        <span class="keyword">int</span> evenSum = <span class="number">0</span>; <span class="comment">//偶数</span></span><br><span class="line">        <span class="comment">//初始化 --条件判断---迭代</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt;= <span class="number">100</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (i % <span class="number">2</span> == <span class="number">0</span>) &#123;</span><br><span class="line">                evenSum += i;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                oddSum += i;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;evenSum:&quot;</span>+evenSum + <span class="string">&quot;---oddSum:&quot;</span> + oddSum);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>


<blockquote>
<p>IDEA使用小技巧<code>输入 100.for 自动生成一个for循环语句</code></p>
</blockquote>
<p>知识点：</p>
<ol>
<li>最先执行初始化步骤，可以声明一种类型，但可初始化一个或者多个循环控制变量，可以是空语句。</li>
<li>然后检测布尔表达式的值，如果为true，循环体被执行，如果是false，循环终止，开始执行循环体后面的语句，布尔表达式也可以去掉</li>
<li>再次检测布尔表达式，循环执行上面过程。</li>
</ol>
<p>示例代码</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//死循环(只要判断条件一直是true就是死循环)</span></span><br><span class="line"><span class="keyword">for</span>(;;)&#123;</span><br><span class="line">    <span class="comment">//TODO</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>实现一个用for循环设计，打印出来能被5整除的数，且每3行换行一次</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> struct;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ForDemo2</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> line = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt;= <span class="number">1000</span>; i++) &#123;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (i % <span class="number">5</span> == <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> (line &gt; <span class="number">2</span>) &#123;</span><br><span class="line">                    System.out.println();</span><br><span class="line">                    line = <span class="number">0</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                line++;</span><br><span class="line">                System.out.print(<span class="string">&quot;-&quot;</span> + i + <span class="string">&quot;-&quot;</span>);</span><br><span class="line"></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>示例代码，打印一个九九乘法口诀</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> struct;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ForDemo3</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=<span class="number">9</span>;i++)&#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">1</span>; j &lt;= i; j++) &#123;</span><br><span class="line">                System.out.print(i+<span class="string">&quot;*&quot;</span>+j+<span class="string">&quot;=&quot;</span>+(i*j) +<span class="string">&quot;\t&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="增强for循环"><a href="#增强for循环" class="headerlink" title="增强for循环"></a>增强for循环</h3><p>在jdk5引入的，主要用于数组和集合的遍历，语法格式为</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">for</span>(声明语句:表达式)&#123;</span><br><span class="line">  <span class="comment">//TODO</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<ol>
<li>声明语句，用来声明新的局部变量，改变量类型必须和数组元素的类型匹配。作用域限定在循环语句块，值与此事数组元素的值相等，表达式是要访问的数组名，或者是返回值为数组的方法。</li>
</ol>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> struct;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ForDeme4</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> [] numbers = &#123;<span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>,<span class="number">40</span>,<span class="number">50</span>&#125;; <span class="comment">//定义一个数组</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> x:numbers)&#123;</span><br><span class="line">            System.out.println(x);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>备注：在python里面for循环用户和Java不一样</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">objs = Book.Objects.<span class="built_in">all</span>().iterator()</span><br><span class="line"><span class="keyword">for</span> obj <span class="keyword">in</span> objs:</span><br><span class="line">  <span class="built_in">print</span>(obj.title)</span><br><span class="line"><span class="keyword">for</span> obj <span class="keyword">in</span> objs:</span><br><span class="line">  <span class="built_in">print</span>(obj.title)</span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">for</span> iterating_var <span class="keyword">in</span> sequence:</span><br><span class="line">   statements(s)</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<h3 id="break-amp-continue"><a href="#break-amp-continue" class="headerlink" title="break&amp;continue"></a>break&amp;continue</h3><p>1.<code> break</code>在任何循环语句的主体部分，均可用<code>break</code>控制循环的流程。Break用于强行退出循环，不执行循环中剩余的语句。在<code>switch</code>语句也可以使用</p>
<ol start="2">
<li><code>continue</code>语句用在循环语句体重，用于终止某次循环过程，既跳过循环体还未执行的语句，并接着进行<code>下一次</code>是否执行循环的判断。</li>
</ol>
<p>在java可以通过continue实现goto的效果，示例代码：</p>
<p>使用一个<code>label</code>来进行跳转的指定的地方，只是不建议使用。</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> struct;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LabelDemo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//打印101-500之间的所有质数</span></span><br><span class="line">        <span class="comment">//质数是指在大于1的自然数中，除了1和它本身以外不再有其他因数的自然数。</span></span><br><span class="line">        <span class="keyword">int</span> count = <span class="number">0</span>;</span><br><span class="line">        <span class="comment">//不建议使用</span></span><br><span class="line">        outer:</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">101</span>; i &lt;= <span class="number">150</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">2</span>; j &lt; i / <span class="number">2</span>; j++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (i % j == <span class="number">0</span>) &#123;</span><br><span class="line">                    <span class="keyword">continue</span> outer;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.print(i + <span class="string">&quot;\t&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<h3 id="实践练习"><a href="#实践练习" class="headerlink" title="实践练习"></a>实践练习</h3><p>使用for循环语句，打印一个三角形</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestDemo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//打印三角形</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= <span class="number">5</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j=<span class="number">5</span>; j&gt;i;j--)&#123;</span><br><span class="line">                System.out.print(<span class="string">&quot; &quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j=<span class="number">1</span>; j&lt;=i;j++)&#123;</span><br><span class="line">                System.out.print(<span class="string">&quot;*&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j=<span class="number">1</span>; j&lt;i;j++)&#123;</span><br><span class="line">                System.out.print(<span class="string">&quot;*&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">谭自财</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://tanzicai.gitee.io/2022/02/09/Java%E6%B5%81%E7%A8%8B/">https://tanzicai.gitee.io/2022/02/09/Java流程/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://tanzicai.gitee.io" target="_blank">小谭的部落阁</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Java/">Java</a><a class="post-meta__tags" href="/tags/%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/">流程控制</a></div><div class="post_share"><div class="social-share" data-image="https://gitee.com/tanzicai/file-pan/raw/master/img/20220209170711.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/02/09/%E6%96%B9%E6%B3%95%E8%AF%A6%E8%A7%A3/"><img class="prev-cover" src="/img/WHT.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Java方法详解</div></div></a></div><div class="next-post pull-right"><a href="/2022/02/09/JAVA%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95/"><img class="next-cover" src="https://gitee.com/tanzicai/file-pan/raw/master/img/20220209165412.jpeg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Java知识</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2021/03/21/JavaCore实践-时钟显示/" title="JavaCore实践-时钟显示"><img class="cover" src="/img/survivors_v02_5120x2880.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-03-21</div><div class="title">JavaCore实践-时钟显示</div></div></a></div><div><a href="/2021/03/21/Java学习路线/" title="Java学习路线"><img class="cover" src="/img/CTTN.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-03-21</div><div class="title">Java学习路线</div></div></a></div><div><a href="/2022/02/09/JAVA基础语法/" title="Java知识"><img class="cover" src="https://gitee.com/tanzicai/file-pan/raw/master/img/20220209165412.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-09</div><div class="title">Java知识</div></div></a></div><div><a href="/2022/02/10/常用类/" title="Java常用类"><img class="cover" src="https://gitee.com/tanzicai/file-pan/raw/master/img/20220209171157.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-10</div><div class="title">Java常用类</div></div></a></div><div><a href="/2022/02/09/异常/" title="Java异常"><img class="cover" src="/img/survivors_v03_5120x2880.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-09</div><div class="title">Java异常</div></div></a></div><div><a href="/2022/02/09/面向对象编程/" title="Java面向对象编程"><img class="cover" src="/img/CTTN.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-09</div><div class="title">Java面向对象编程</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="https://gitee.com/tanzicai/drawingbed/raw/master/img/20210609205810.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">谭自财</div><div class="author-info__description">你别卷入世俗，我别沉迷初遇</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">35</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">12</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/tanzicai"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/tanzicai" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="https://gitee.com/tanzicai" target="_blank" title="Github"><i class="fab fa-git-square"></i></a><a class="social-icon" href="http://wpa.qq.com/msgrd?v=3&amp;uin=1728561744&amp;site=qq&amp;menu=yes" target="_blank" title="QQ"><i class="fas fa-qq"></i></a><a class="social-icon" href="mailto:1728561744@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">本站正在切换主题测试，如有错误提示，请联系我，谢谢</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Java%E6%B5%81%E7%A8%8B"><span class="toc-number">1.</span> <span class="toc-text">Java流程</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E8%BE%93%E5%85%A5%E7%B1%BB-Scanner"><span class="toc-number">2.</span> <span class="toc-text">输入类-Scanner</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Scanner%E5%AE%9E%E4%BE%8B"><span class="toc-number">2.1.</span> <span class="toc-text">Scanner实例</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%A1%BA%E5%BA%8F%E7%BB%93%E6%9E%84"><span class="toc-number">2.2.</span> <span class="toc-text">顺序结构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%80%89%E6%8B%A9%E7%BB%93%E6%9E%84"><span class="toc-number">2.3.</span> <span class="toc-text">选择结构</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#if-%E5%8D%95%E9%80%89%E6%8B%A9%E7%BB%93%E6%9E%84"><span class="toc-number">2.3.1.</span> <span class="toc-text">if 单选择结构</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#if-%E5%8F%8C%E9%80%89%E6%8B%A9%E7%BB%93%E6%9E%84"><span class="toc-number">2.3.2.</span> <span class="toc-text">if 双选择结构</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#if-%E5%A4%9A%E9%80%89%E6%8B%A9%E7%BB%93%E6%9E%84"><span class="toc-number">2.3.3.</span> <span class="toc-text">if 多选择结构</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B5%8C%E5%A5%97%E7%9A%84if%E7%BB%93%E6%9E%84"><span class="toc-number">2.3.4.</span> <span class="toc-text">嵌套的if结构</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#switch%E5%A4%9A%E9%80%89%E6%8B%A9%E7%BB%93%E6%9E%84"><span class="toc-number">2.3.5.</span> <span class="toc-text">switch多选择结构</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BE%AA%E7%8E%AF%E7%BB%93%E6%9E%84"><span class="toc-number">2.4.</span> <span class="toc-text">循环结构</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#while%E5%BE%AA%E7%8E%AF"><span class="toc-number">2.4.1.</span> <span class="toc-text">while循环</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#do%E2%80%A6while%E5%BE%AA%E7%8E%AF"><span class="toc-number">2.4.2.</span> <span class="toc-text">do…while循环</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#for%E5%BE%AA%E7%8E%AF"><span class="toc-number">2.4.3.</span> <span class="toc-text">for循环</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A2%9E%E5%BC%BAfor%E5%BE%AA%E7%8E%AF"><span class="toc-number">2.4.4.</span> <span class="toc-text">增强for循环</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#break-amp-continue"><span class="toc-number">2.4.5.</span> <span class="toc-text">break&amp;continue</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E8%B7%B5%E7%BB%83%E4%B9%A0"><span class="toc-number">2.4.6.</span> <span class="toc-text">实践练习</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2022/02/10/%E5%B8%B8%E7%94%A8%E7%B1%BB/" title="Java常用类"><img src="https://gitee.com/tanzicai/file-pan/raw/master/img/20220209171157.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java常用类"/></a><div class="content"><a class="title" href="/2022/02/10/%E5%B8%B8%E7%94%A8%E7%B1%BB/" title="Java常用类">Java常用类</a><time datetime="2022-02-10T12:25:00.000Z" title="发表于 2022-02-10 20:25:00">2022-02-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/09/%E5%BC%82%E5%B8%B8/" title="Java异常"><img src="/img/survivors_v03_5120x2880.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java异常"/></a><div class="content"><a class="title" href="/2022/02/09/%E5%BC%82%E5%B8%B8/" title="Java异常">Java异常</a><time datetime="2022-02-09T13:25:00.000Z" title="发表于 2022-02-09 21:25:00">2022-02-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/09/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/" title="Java面向对象编程"><img src="/img/CTTN.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java面向对象编程"/></a><div class="content"><a class="title" href="/2022/02/09/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/" title="Java面向对象编程">Java面向对象编程</a><time datetime="2022-02-09T13:25:00.000Z" title="发表于 2022-02-09 21:25:00">2022-02-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/09/%E6%95%B0%E7%BB%84%E8%AF%A6%E8%A7%A3/" title="Java数组详解"><img src="/img/CNYN.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java数组详解"/></a><div class="content"><a class="title" href="/2022/02/09/%E6%95%B0%E7%BB%84%E8%AF%A6%E8%A7%A3/" title="Java数组详解">Java数组详解</a><time datetime="2022-02-09T13:25:00.000Z" title="发表于 2022-02-09 21:25:00">2022-02-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/09/%E6%96%B9%E6%B3%95%E8%AF%A6%E8%A7%A3/" title="Java方法详解"><img src="/img/WHT.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java方法详解"/></a><div class="content"><a class="title" href="/2022/02/09/%E6%96%B9%E6%B3%95%E8%AF%A6%E8%A7%A3/" title="Java方法详解">Java方法详解</a><time datetime="2022-02-09T12:25:00.000Z" title="发表于 2022-02-09 20:25:00">2022-02-09</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://gitee.com/tanzicai/file-pan/raw/master/img/20220209170711.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By 谭自财</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">欢迎进入 <a href="https://tanzicai.gitee.io">小谭的部落阁</a>!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="font-plus" type="button" title="放大字体"><i class="fas fa-plus"></i></button><button id="font-minus" type="button" title="缩小字体"><i class="fas fa-minus"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>